package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * https://leetcode.cn/problems/find-all-anagrams-in-a-string/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-26 20:55
 */
public class Test09 {
    public static void main(String[] args) {
        System.out.println(findAnagrams("abab", "ab"));
    }

    public static List<Integer> findAnagrams(String s, String p) {
        int win_len = p.length();
        int point = 0;
        List<Integer> res = new ArrayList<>();
        while (point + win_len - 1 < s.length()) {
            if (check(s.substring(point, point + win_len), p)) {//true为是
                res.add(point);
            }
            point++;
        }

        return res;
    }

    public static boolean check(String a, String b) {
        int[] aChars = new int[26];
        int[] bChars = new int[26];
        for (int i = 0; i < a.length(); i++) {
            aChars[a.charAt(i) - 'a']++;
            bChars[b.charAt(i) - 'a']++;
        }
        for (int i = 0; i < aChars.length; i++) {
            if (aChars[i] != bChars[i]) {
                return false;
            }
        }

        return true;
    }

}
